package com.mt.order.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.order.entity.OrderDetailEntity;
import com.mt.order.entity.OrderEntity;
import com.mt.order.entity.OrderTradeEntity;
import com.mt.order.mapper.OrderDetailEntityMapper;
import com.mt.order.mapper.OrderEntityMapper;
import com.mt.order.mapper.OrderTradeEntityMapper;

@Component
public class OrderServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(OrderServiceModel.class);

    @Autowired
    private OrderEntityMapper orderEntityMapper;

    @Autowired
    private OrderDetailEntityMapper orderDetailEntityMapper;

    @Autowired
    private OrderTradeEntityMapper orderTradeEntityMapper;

    public ServiceResult<List<OrderEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<List<OrderEntity>> result = new ServiceResult<List<OrderEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.orderEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<OrderEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<OrderEntity> result = new ServiceResult<OrderEntity>();
        try {
            result.setResult(this.orderEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }
    public ServiceResult<Integer> updateList(Map<String, Object> params) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result.setResult(this.orderEntityMapper.updateList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][updateList]：query updateList occur exception", e);
        }
        return result;
    }
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> insert(OrderEntity orderEntity) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderEntityMapper.insert(orderEntity);
            if (save > 0) {
                id = orderEntity.getOrderId();
                if (orderEntity.getOrderDetailList() != null && orderEntity.getOrderDetailList().size() > 0) {
                    List<OrderDetailEntity> orderDetailList = orderEntity.getOrderDetailList();
                    for (int i = 0; i < orderDetailList.size(); i++) {
                        OrderDetailEntity orderDetailEntity = orderDetailList.get(i);
                        orderDetailEntity.setOrderId(id);
                    }
                    orderDetailEntityMapper.insertList(orderDetailList);
                }
                if (orderEntity.getOrderTradeList() != null && orderEntity.getOrderTradeList().size() > 0) {
                    List<OrderTradeEntity> orderTradeList = orderEntity.getOrderTradeList();
                    for (int i = 0; i < orderTradeList.size(); i++) {
                        OrderTradeEntity orderTradeEntity = orderTradeList.get(i);
                        orderTradeEntity.setOrderId(id);
                    }
                    orderTradeEntityMapper.insertList(orderTradeList);
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][insert]：query insert occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<List<OrderEntity>> insertList(List<OrderEntity> orderEntityList) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<List<OrderEntity>> result = new ServiceResult<List<OrderEntity>>();
        int id = 0;
        try {
            for (OrderEntity orderEntity : orderEntityList) {
                int save = this.orderEntityMapper.insert(orderEntity);
                if (save > 0) {
                    id = orderEntity.getOrderId();
                    if (orderEntity.getOrderDetailList() != null && orderEntity.getOrderDetailList().size() > 0) {
                        List<OrderDetailEntity> orderDetailList = orderEntity.getOrderDetailList();
                        for (int i = 0; i < orderDetailList.size(); i++) {
                            OrderDetailEntity orderDetailEntity = orderDetailList.get(i);
                            orderDetailEntity.setOrderId(id);
                        }
                        orderDetailEntityMapper.insertList(orderDetailList);
                    }
                    if (orderEntity.getOrderTradeList() != null && orderEntity.getOrderTradeList().size() > 0) {
                        List<OrderTradeEntity> orderTradeList = orderEntity.getOrderTradeList();
                        for (int i = 0; i < orderTradeList.size(); i++) {
                            OrderTradeEntity orderTradeEntity = orderTradeList.get(i);
                            orderTradeEntity.setOrderId(id);
                        }
                        orderTradeEntityMapper.insertList(orderTradeList);
                    }
                }

            }
            result.setResult(orderEntityList);
            System.out.println();
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][insert]：query insert occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> updateRelate(OrderEntity orderEntity) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderEntityMapper.updateByPrimaryKeySelective(orderEntity);
            if (save > 0) {
                id = orderEntity.getOrderId();
                // 编辑操作，先删除原有用户角色关联数据，再创建
                if (orderEntity.getOrderTradeList() != null && orderEntity.getOrderTradeList().size() > 0) {
                    List<OrderTradeEntity> orderTradeList = orderEntity.getOrderTradeList();
                    for (int i = 0; i < orderTradeList.size(); i++) {
                        OrderTradeEntity orderTradeEntity = orderTradeList.get(i);
                        orderTradeEntity.setOrderId(id);
                    }
                    orderTradeEntityMapper.insertList(orderTradeList);
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    /**
     * 此方法用于修改订单支付成功后修改订单状态，订单支付流水状态
     * 
     * @param orderEntity
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> doUpdatePaymentInfo(OrderEntity orderEntity) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderEntityMapper.updateByPrimaryKeySelective(orderEntity);
            if (save > 0) {
                id = orderEntity.getOrderId();
                if (orderEntity.getOrderTradeList() != null && orderEntity.getOrderTradeList().size() > 0) {
                    List<OrderTradeEntity> orderTradeList = orderEntity.getOrderTradeList();
                    for (OrderTradeEntity entity : orderTradeList) {
                        orderTradeEntityMapper.updateByPrimaryKeySelective(entity);
                    }
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(OrderEntity orderEntity) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.orderEntityMapper.updateByPrimaryKeySelective(orderEntity);
            if (save > 0) {
                id = orderEntity.getOrderId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer orderId) {
        Assert.notNull(this.orderEntityMapper, "Property 'orderEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.orderEntityMapper.deleteByPrimaryKey(orderId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[OrderServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
